﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Network.StepProtocol;

public abstract class StepPackageBase : Package
{
    public StepPackageBase()
        :base(PackageTypeType.Step)
    {
        Head = new StepHead();
        Tail = new StepTail();
    }
    public abstract void Prepare(long sessionID, bool messageChain, int msgSeqNum);
    public void MakePackage(StringBuilder s)
    {
        StringBuilder sb = new StringBuilder();
        ToProtocolStream(sb);
        var body = Encoding.UTF8.GetBytes(sb.ToString());
        Head.BodyLen = (ushort)body.Length;
        StepUtility.HeadToStream(s, Head);
        s.Append(sb);
        var bytes = Encoding.UTF8.GetBytes(s.ToString());
        Tail.CheckSum = StepUtility.CalculateSum(bytes, 0, bytes.Length);
        StepUtility.TailToStream(s, Tail);
    }
    public abstract void ToProtocolStream(StringBuilder s);
    public abstract bool FromProtocolStream(byte[] s, int startIndex, int endIndex);
    public abstract void GetDebugString(StringBuilder s);

    public StepHead Head;
    public StepTail Tail;
}
